<template>
  <div>
    <el-dialog
      :title="status.isAdd ? '角色添加' : '角色编辑'"
      :visible.sync="status.isshow"
      @closed="closed"
    >
      <el-form :model="form">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" label-width="120px">
          <el-tree
            :data="data"
            show-checkbox
            default-expand-all
            node-key="id"
            ref="tree"
            highlight-curren
            :props="{ children: 'children', label: 'title' }"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="status.isshow = false">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="con"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="updata">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqmenulist,
  reqroleadd,
  reqroleinfo,
  reqroleedit,
} from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";

export default {
  components: {},
  props: ["status"],
  data() {
    return {
      isshow: true,
      form: {
        rolename: "",
        menus: "",
        status: 1,
      },
      data: [],
      defaultProps: {
        children: "children",
        label: "data.title",
      },
    };
  },
  methods: {
    ...mapActions(["changeInfo"]),
    con() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      //发送请求
      reqroleadd(this.form).then((res) => {
        this.checkeddata().then(() => {
          if (res.data.code == 200) {
            // 弹框
            successAlert(res.data.msg);

            // 2.form的数据清空一下
            this.empty();
            // 3.弹框消失
            this.status.isshow = false;
            // 4.列表需要重新渲染
            this.$emit("init");
          }
        });
      });
    },
    //验空
    checkeddata() {
      return new Promise((reslove) => {
        if (this.form.rolename == "") {
          errorAlert("角色名称不能为空");
          return;
        }

        reslove();
      });
    },
    getOne(id) {
      reqroleinfo({
        id: id,
      }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          console.log(this.form);
          this.$refs.tree.setCheckedKeys(JSON.parse(this.form.menus));
          this.form.id = id;
        }
      });
    },
    empty() {
      this.form = {
        rolename: "",
        menus: "",
        status: 1,
      };
    },
    updata() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      console.log(this.form);
      reqroleedit(this.form).then((res) => {
        this.checkeddata().then(() => {
          if (res.data.code == 200) {
            // 1.弹成功
            successAlert(res.data.msg);
            // 2.form的数据清空一下

            //判断是不是自己
            if (this.form.id === this.info.roleid) {
              //清除数据
              this.changeInfo({});
              //退出登录
              this.$router.replace("/login");
              return;
            }
            this.empty();
            // 3.弹框消失
            this.status.isshow = false;
            // 4.列表需要重新渲染
            this.$emit("init");
          }
        });
      });
    },
    // 9.弹框关闭
    closed() {
      // 如果是添加的取消，什么都不做；如果是编辑的取消，form清空
      if (!this.status.isAdd) {
        this.empty();
        this.$refs.tree.setCheckedKeys([]);
      }
    },
  },

  mounted() {
    //获取数据
    reqmenulist({
      istree: true,
    }).then((res) => {
      if (res.data.code == 200) {
        this.data = res.data.list;
      }
    });
  },
  computed: { ...mapGetters(["info"]) },
  watch: {},
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>
